from random import randint
from time import sleep
from selenium import webdriver
from selenium.webdriver.support.select import Select
import unittest
from HTMLTestRunner import HTMLTestRunner


class jxc_ceshi(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.get("http://localhost/lzj/index.asp")

    def tearDown(self):
        self.driver.quit()

# 登录测试
    def test_login(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        self.assertIn('后台管理系统', self.driver.page_source)

# 登陆
    def login(self):
        self.driver.find_element_by_name('username').send_keys('admin')
        self.driver.find_element_by_name('pwd').send_keys('admin')
        self.driver.find_element_by_name('enter').click()
        sleep(2)

# 删除全部单位测试
    def test_delete_units(self):
        self.login()
        self.driver.switch_to.frame('left')
        self.driver.find_element_by_xpath('/html/body/table/tbody/tr[13]/td').click()
        self.driver.find_element_by_xpath('/html/body/table/tbody/tr[14]/td/table/tbody/tr[7]/td').click()
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_id('chkall').click()
        self.driver.find_element_by_css_selector('input.button:nth-child(2)').click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        self.assertNotIn("name='ID'", self.driver.page_source)

# 添加单位测试
    def test_insert_unit(self):
        self.login()
        self.driver.switch_to.frame('left')
        self.driver.find_element_by_xpath('/html/body/table/tbody/tr[13]/td').click()
        self.driver.find_element_by_xpath('/html/body/table/tbody/tr[14]/td/table/tbody/tr[7]/td').click()
        sleep(2)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_css_selector('input.button:nth-child(1)').click()
        sleep(2)
        self.driver.find_element_by_name('danwei').send_keys('个')
        self.driver.find_element_by_name('submit').click()
        sleep(2)
        self.driver.switch_to.alert.accept()
        self.driver.switch_to.frame('right')
        self.assertIn('name="ID"', self.driver.page_source)

# 添加大类测试
    def test_insert_bigclass(self):
        self.login()
        self.driver.switch_to.frame('left')
        self.driver.find_element_by_xpath('/html/body/table/tbody/tr[13]/td').click()
        self.driver.find_element_by_xpath('/html/body/table/tbody/tr[14]/td/table/tbody/tr[5]/td').click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_css_selector('input.button:nth-child(1)').click()
        self.driver.find_element_by_name('bigclass').send_keys('测试')
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.assertIn('name="ID"', self.driver.page_source)

# 添加小类测试
    def test_insert_smallclass(self):
        self.login()
        self.driver.switch_to.frame('left')
        self.driver.find_element_by_xpath('/html/body/table/tbody/tr[13]/td').click()
        self.driver.find_element_by_xpath('/html/body/table/tbody/tr[14]/td/table/tbody/tr[6]/td').click()
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.driver.find_element_by_css_selector('input.button:nth-child(1)').click()
        sleep(1)
        sel_list = Select(self.driver.find_element_by_name('bigclass'))
        rand = randint(0, len(sel_list.options) - 1)
        sel_list.select_by_index(rand)
        self.driver.find_element_by_name('smallclass').send_keys('测试')
        self.driver.find_element_by_name('submit').click()
        sleep(1)
        self.driver.switch_to.alert.accept()
        sleep(1)
        self.driver.switch_to.default_content()
        self.driver.switch_to.frame('right')
        self.assertIn('name="ID"', self.driver.page_source)

# 测试集合
def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(jxc_ceshi('test_login'))
    suite.addTest(jxc_ceshi('test_delete_units'))
    suite.addTest(jxc_ceshi('test_insert_unit'))
    suite.addTest(jxc_ceshi('test_insert_bigclass'))
    suite.addTest(jxc_ceshi('test_insert_smallclass'))
    return suite

# 指明测试报告文件
with open('d:/lzj.html', 'wb') as f:
    runner = HTMLTestRunner(stream=f, title='测试报告')
    runner.run(test_suite())
